<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: GetCircle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\circle;

use app\api\model\dao\Circle;
use app\api\model\dao\CircleCategory;
use app\api\model\data\circle\CircleData;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;
use think\Validate;

class GetCircleList extends PageBase
{

    private $objCircle;
    private $objCircleCategory;
    private $objCircleData;

    public function __construct()
    {
        parent::__construct();

        $this->objCircle = new Circle();
        $this->objCircleCategory = new CircleCategory();
        $this->objCircleData = new CircleData();

    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'tab' => 'require|number',
        ];
        $msg = [
            'tab.require' => '请选择分类tab',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    /**
     * page 入口
     *
     * @param $param
     * @return array
     * @throws Exception
     */
    public function myExcute($param)
    {
        $this->checkToken($param);

        $this->user_id = !empty($this->user_id) ? $this->user_id : 0;

        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            throw new Exception($ret, Error::ERR_PARAM);
        }

        // 2、获取哪种的圈子，默认全部
        $categoryRet = $this->getCircleCategoryFromDB();
        if (false === $categoryRet) {
            throw new Exception('get circle category from db fail. ', Error::ERR_SYS);
        }
        $arrCategoryDefault = [
            [
                'id' => Flag::CONST_CIRCLE_TAB_FOLLOW,
                'name' => '我关注',
            ],
            [
                'id' => Flag::CONST_CIRCLE_TAB_HOT,
                'name' => '最热',
            ],
            [
                'id' => Flag::CONST_CIRCLE_TAB_NEW,
                'name' => '最新',
            ],
        ];
        if (empty($categoryRet)) {
            $categoryRet = [];
        }
        $categoryRet = array_merge($arrCategoryDefault, $categoryRet);
        $this->data['tab'] = $categoryRet;
        $this->data['list'] = [];
        if (in_array($param['tab'], Flag::$arr_circle_tab)) {
            switch ($param['tab']) {
                case Flag::CONST_CIRCLE_TAB_ALL:
                    break;
                case Flag::CONST_CIRCLE_TAB_FOLLOW:
                    $this->data['list'] = $this->objCircleData->followCircle($this->user_id, $this->offset, $this->limit);
                    break;
                case Flag::CONST_CIRCLE_TAB_HOT:
                    $this->data['list'] = $this->objCircleData->hotCircle($this->offset, $this->limit);
                    break;
                case Flag::CONST_CIRCLE_TAB_NEW:
                    $this->data['list'] = $this->objCircleData->newCircle(0, $this->offset, $this->limit);
                    break;
                case Flag::CONST_CIRCLE_TAB_MYCREATE:
                    $this->data['list'] = $this->objCircleData->mineCircle($this->user_id, $this->offset, $this->limit);
                    break;
            }
        } else {
            // 3、从数据库中获取圈子详情
            $this->data['list'] = $this->objCircleData->getCircleListByCategory($param['tab'], $this->offset, $this->limit);
        }
    }

    /**
     * 从数据库中获取圈子分类
     *
     * @param $cir_id
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public function getCircleCategoryFromDB()
    {
        $arrCond = [
            'is_delete' => ['=', Flag::CONST_CIRCLECATEGORY_DELETE_NOT]
        ];

        $ret = $this->objCircleCategory->getCircleCategory($arrCond, ['id', 'name'], 0, 10, ' rank asc ');
        if (false === $ret) {
            return false;
        }
        return $ret;
    }
}
